
package PV112;


import static PV112.CameraType.CABIN;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.opengl.util.FPSAnimator;
import java.awt.event.MouseEvent;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;


public class MainWindow extends javax.swing.JFrame {
    private final GLJPanel glPanel;
    private GLProfile profile = GLProfile.get(GLProfile.GL2);
    private OpenGlListener openGlListener = new OpenGlListener();

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        initComponents();
        
        // vytvori sa viditelny panel na ktorom sa bude zobrazovat nas graficky vystup
        final GLCapabilities capabilities = new GLCapabilities(profile);
        capabilities.setSampleBuffers(true);
        capabilities.setNumSamples(8);
        
        glPanel = new GLJPanel(new GLCapabilities(profile));
        
        // tento panel sa umiestni na halvne okno aplikacie
        add(glPanel);
        
        final FPSAnimator animator = new FPSAnimator(glPanel, 30, true);
        animator.start();
        // rozmery glPanela sa nastavia tak, aby sa zhodovali s rozmermi hlavneho okna
        glPanel.setSize(getWidth() - getInsets().left - getInsets().right,
                        getHeight() - getInsets().top - getInsets().bottom);
        
        // k glPanelu pripojime OpenGLListener, aby sme mohli reagovat na udalosti
        // generovane tymto panelom
        glPanel.addGLEventListener(openGlListener);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CraneGL");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(1200, 1024));
        addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                formMouseWheelMoved(evt);
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 816, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 619, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // zmena velkosti okna
    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        glPanel.setSize(getWidth() - getInsets().left - getInsets().right,
                        getHeight() - getInsets().top - getInsets().bottom);
    }//GEN-LAST:event_formComponentResized

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        switch (evt.getKeyCode()){
            //r
            case 82:
                openGlListener.rotateCrane(-3.5f, glPanel.getGL().getGL2());
                break;
            //t
            case 84:    
                openGlListener.rotateCrane(3.5f, glPanel.getGL().getGL2());
                break;
            //i
            case 73:
                openGlListener.moveMagnet(.2f, glPanel.getGL().getGL2());
                break;
            //k
            case 75:
                openGlListener.moveMagnet(-.2f, glPanel.getGL().getGL2());
                break;
            //j
            case 74:
                openGlListener.moveRail(-.2f, glPanel.getGL().getGL2());
                break;
            //l
            case 76:
                openGlListener.moveRail(.2f, glPanel.getGL().getGL2());
                break;
            //m
            case 77:
                openGlListener.toggleMagnet();
                break;
            //c
            case 67:
                switch (openGlListener.getCameraType()){
                    case FREE:
                        openGlListener.setCameraType(CameraType.CABIN);
                        break;
                    case CABIN:
                        openGlListener.setCameraType(CameraType.ROPE);
                        break;
                    case ROPE:
                        openGlListener.setCameraType(CameraType.FREE);
                        break;
                };
                break;
            //x
            case 88:
                switch (openGlListener.getCameraType()){
                    case FREE:
                        openGlListener.setCameraType(CameraType.ROPE);
                        break;
                    case CABIN:
                        openGlListener.setCameraType(CameraType.FREE);
                        break;
                    case ROPE:
                        openGlListener.setCameraType(CameraType.CABIN);
                        break;
                };
                break;
            //Left
            case 37:
                openGlListener.moveHorizontally(-.2f, glPanel.getGL().getGL2());
                break;
            //Right
            case 39:
                openGlListener.moveHorizontally(+.2f, glPanel.getGL().getGL2());
                break;
            //Up
            case 38:
                openGlListener.moveVerically(.2f, glPanel.getGL().getGL2());
                break;
            //Down
            case 40:
                openGlListener.moveVerically(-.2f, glPanel.getGL().getGL2());
                break;
        }
        
    }//GEN-LAST:event_formKeyPressed

    private void formMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_formMouseWheelMoved
            openGlListener.zoom(evt.getPreciseWheelRotation(), glPanel.getGL().getGL2());
    }//GEN-LAST:event_formMouseWheelMoved

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed

    }//GEN-LAST:event_formMousePressed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased

    }//GEN-LAST:event_formMouseReleased

    private int prevX = 0, prevY = 0;
    
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved

    }//GEN-LAST:event_formMouseMoved

    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged

        //16 - left, 4 - right mouse btn
                if (evt.getModifiers() == 16){
                        if (evt.getX() > prevX){
                        openGlListener.moveHorizontally(.1f, glPanel.getGL().getGL2());
                    }else{
                        openGlListener.moveHorizontally(-.1f, glPanel.getGL().getGL2());
                    }
                }else if (evt.getModifiers() == 4){
                if (evt.getY() > prevY){
                    openGlListener.moveVerically(.1f, glPanel.getGL().getGL2());
                }else{
                    openGlListener.moveVerically(-.1f, glPanel.getGL().getGL2());
                }
            }
                prevX = evt.getX();
                prevY = evt.getY();
//            }
            
    }//GEN-LAST:event_formMouseDragged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
